package io.litchi.auth.Infrastructure.handler;

import io.litchi.auth.Infrastructure.config.AuthConfig;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver;


/**
 * description: 自定义请求头token解析器
 *
 * @author helang
 * @since 2025-09-11
 */
@AllArgsConstructor
public class LitchiBearerTokenResolver implements BearerTokenResolver {

    private AuthConfig authConfig;

    @Override
    public String resolve(HttpServletRequest request) {

        String token = request.getHeader(authConfig.getTokenKey());
        if(token == null||token.isEmpty()){
            return null;
        }
        return handlerToken(token,authConfig.getTokenPrefix());
    }
    public static String handlerToken(String token,String tokenPrefix){
        return token.trim().replaceAll(tokenPrefix, "");
    }
}